package com.tianyuan.lims.utils;

import java.util.*;

public class StatisticsUtil {
    public static List<String> toData(String d) {
        return Arrays.asList(d.replace("\'", "").replace(" ", "").split(","));
    }

    public static List<Map<String, Object>> toPairColorData(List<String> pairData) {
        List<NameValueColorPair> data = new Vector<>();
        for (String s : pairData) {
            data.add(new NameValueColorPair(s));
        }
        // 将数据转换为ECharts需要的格式
        List<Map<String, Object>> seriesData = new ArrayList<>();
        for (NameValueColorPair pair : data) {
            Map<String, Object> map = new HashMap<>();
            map.put("name", pair.getName());
            map.put("value", pair.getValue());
            map.put("itemStyle", pair.getColor());
            seriesData.add(map);
        }
        return seriesData;
    }

    public static class NameValueColorPair {
        private String name;
        private int value;
        private Map<String, Object> color = new HashMap<>();

        public NameValueColorPair(String n, int v, String c) {
            name = n;
            value = v;
            color.put("color", c);
        }

        public NameValueColorPair(String s) {
            String[] a = s.split(",");
            name = a[0];
            if (a.length > 0) {
                a[1] = a[1].replace(" ", "");
                value = Integer.parseInt(a[1]);
            }
            if (a.length > 1) color.put("color", a[2]);
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Map<String, Object> getColor() {
            return color;
        }

        public void setColor(Map<String, Object> color) {
            this.color = color;
        }
    }

    public static class NameValuePair {
        private String name;
        private int value;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }
}
